package com.michaelbulava.TFS.vcs.internal.providers;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.EditFileProvider;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.log.FileLogger;
import com.michaelbulava.TFS.vcs.exceptions.NotConnectedToTFS;
import com.michaelbulava.TFS.vcs.exceptions.TFSException;
import com.michaelbulava.TFS.vcs.internal.SharedOperations;
import com.michaelbulava.TFS.vcs.internal.WorkstationHelper;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;
import com.michaelbulava.TFS.vcs.utils.TFSUtil;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by MichaelA on 8/18/2015.
 */
public class TFSEditFileProvider implements EditFileProvider {
    private final Project myProject;
    private final Logger log = FileLogger.getInstance(TFSEditFileProvider.class);

    public TFSEditFileProvider(Project project) {
        this.myProject = project;
    }


    @Override
    public void editFiles(VirtualFile[] files) throws VcsException {
        log.info(String.format("Entering TFSEditFileProvider.editFiles(VirtualFile[]) with %d files passed", files.length));
        final Collection<VcsException> errors = new ArrayList<>();
        List<FilePath> myFiles = TFSFileUtil.getFilePaths(files);
        try{
            Collection<FilePath> orphans = WorkstationHelper.processByWorkspaces(myFiles, false,
                    myProject, new WorkstationHelper.VoidDelegateProcess() {
                        @Override
                        public void executeRequest(Workspace workspace, List<FilePath> localPaths) throws TFSException {
                            errors.addAll(SharedOperations.performCheckout(myProject, workspace, localPaths));
                        }
                    }) ;
            if (!orphans.isEmpty()){
                StringBuilder sb = new StringBuilder(Messages.getString("TFSEditFileProvider.OrphanFileError"));
                for(FilePath file : orphans){
                    sb.append(file.getPresentableUrl()).append("\n");
                }
                throw new VcsException(sb.toString());
            }
        } catch (NotConnectedToTFS e){
            log.error("Not Connected exception thrown", e);
            try {
                TFSFileUtil.setReadonly(myFiles, false);
            } catch (IOException e1) {
                errors.add(new VcsException(e1));
            }
        } catch (TFSException e) {
            log.error("TFSException thrown", e);
            errors.add(new VcsException(e));
        }
        if (!errors.isEmpty()){
            VcsException exception = TFSUtil.collectionException(errors);
            throw exception;
        }
    }

    @Override
    public String getRequestText() {
        return Messages.getString("TFSEditFileProvider.RequestText");
    }
}
